package com.sky.controller.admin;


import com.sky.annotation.Log;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.BusinessType;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    DishService dishService;

    @Autowired
    RedisTemplate redisTemplate;

    // 修改菜品
    @PutMapping
    @Log(title = "修改菜品",businessType = BusinessType.UPDATE)
    public Result updateDish(@RequestBody DishDTO dishOTD) {
        dishService.updateDish(dishOTD);
        // 2.清楚缓存
        cleanCache("dish_*");
        return Result.success();
    }

    // 批量删除菜品
    @DeleteMapping
    @Log(title = "批量删除菜品",businessType = BusinessType.DELETE)
    public Result deleteDishes(@RequestParam List<String> ids) {
        dishService.deleteDishes(ids);
        // 2.清楚缓存
        cleanCache("dish_*");
        return Result.success();
    }

    // 新增菜品
    @PostMapping
    @Log(title = "添加菜品",businessType = BusinessType.UPDATE)
    public Result addDish(@RequestBody DishDTO dishDTO) {
        dishService.addDish(dishDTO);
        cleanCache("dish_"+dishDTO.getCategoryId());
        return Result.success();
    }


    // 根据id查询菜品
    @GetMapping("/{id}")
    @Log(title = "根据id查询菜品",businessType = BusinessType.QUERY)
    public Result getDish(@PathVariable String id) {
        DishVO dishVO = dishService.getDish(id);
        return Result.success(dishVO);
    }

    //根据分类id查询菜品
    @GetMapping("/list")
    @Log(title = "根据分类id查询菜品",businessType = BusinessType.QUERY)
    public Result getDishList(String categoryId) {
        List<Dish> dishList = dishService.getDishList(categoryId);
        return Result.success(dishList);
    }

    //菜品分页查询
    @GetMapping("/page")
    public Result pageDish(DishPageQueryDTO dishPageQueryDTO) {
        PageBean<DishVO> pageBean = dishService.pageDish(dishPageQueryDTO);
        return Result.success(pageBean);
    }

    //菜品起售、停售
    @PostMapping("/status/{status}")
    @Log(title = "菜品起售、停售",businessType = BusinessType.CHANGE_STATUS)
    public Result changeStatus(@PathVariable String status, String id) {
        dishService.changeStatus(status, id);
        // 2.清楚缓存
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
